package com.lee.serviceagent.ocr.tesseractsec;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.imgscalr.Scalr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author lee
 * @description class
 * @date 2025/8/19 下午4:06
 */
@Service
@Slf4j
public class OcrService {
    @Autowired
    private Tesseract tesseract;

    public IdCardInfo recognizeIdCard(File imageFile) throws IOException, TesseractException {
        // 预处理图像
        BufferedImage image = preprocessImage(imageFile);

        // 执行OCR识别
        String result = tesseract.doOCR(image);
        log.info(result);
        // 解析识别结果
        return parseIdCardInfo(result);

    }

    private BufferedImage preprocessImage(File imageFile) throws IOException {
        BufferedImage image = ImageIO.read(imageFile);

        // 调整图像大小以提高识别率
        image = Scalr.resize(image, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_WIDTH, 1000);

        // 可以添加更多图像处理步骤，如灰度化、二值化等

        return image;
    }

    private IdCardInfo parseIdCardInfo(String ocrResult) {
        IdCardInfo info = new IdCardInfo();

        // 使用正则表达式提取信息
        extractName(ocrResult, info);
        extractGender(ocrResult, info);
        extractNation(ocrResult, info);
        extractBirthDate(ocrResult, info);
        extractAddress(ocrResult, info);
        extractIdNumber(ocrResult, info);
        extractIssuingAuthority(ocrResult, info);
        extractValidPeriod(ocrResult, info);

        return info;
    }

    private void extractName(String text, IdCardInfo info) {
        Pattern pattern = Pattern.compile("姓名[\\s:：]*([^\\s]+)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            info.setName(matcher.group(1).trim());
        }
    }

    private void extractGender(String text, IdCardInfo info) {
        Pattern pattern = Pattern.compile("性别[\\s:：]*([男女])");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            info.setGender(matcher.group(1).trim());
        }
    }

    private void extractNation(String text, IdCardInfo info) {
        Pattern pattern = Pattern.compile("民族[\\s:：]*([^\\s]+)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            info.setNation(matcher.group(1).trim());
        }
    }

    private void extractBirthDate(String text, IdCardInfo info) {
        Pattern pattern = Pattern.compile("出生[\\s:：]*(\\d{4}年\\d{2}月\\d{2}日)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            info.setBirthDate(matcher.group(1).trim());
        }
    }

    private void extractAddress(String text, IdCardInfo info) {
        Pattern pattern = Pattern.compile("住址[\\s:：]*(.+?)(?=\\s*(公民身份号码|\\n|$))");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            info.setAddress(matcher.group(1).trim());
        }
    }

    private void extractIdNumber(String text, IdCardInfo info) {
        Pattern pattern = Pattern.compile("公民身份号码[\\s:：]*(\\d{17}[\\dXx])");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            info.setIdNumber(matcher.group(1).trim());
        }
    }

    private void extractIssuingAuthority(String text, IdCardInfo info) {
        Pattern pattern = Pattern.compile("签发机关[\\s:：]*(.+?)(?=\\s*(有效期限|\\n|$))");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            info.setIssuingAuthority(matcher.group(1).trim());
        }
    }

    private void extractValidPeriod(String text, IdCardInfo info) {
        Pattern pattern = Pattern.compile("有效期限[\\s:：]*(.+?)(?=\\s*$)");
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            info.setValidPeriod(matcher.group(1).trim());
        }
    }
}
